import { IStageAssetsInfo, Stage } from "babylon-lib/base/stage";
import { ArcRotateCamera } from "@babylonjs/core/Cameras/arcRotateCamera";
import { Scene } from "@babylonjs/core/scene";
import { Camera } from "@babylonjs/core/Cameras/camera";
import { Light } from "@babylonjs/core/Lights/light";
import { RawAssetsUrl, RawData } from "../raw-assets-info";
import { InitializeComponent } from './default-stage-components/initialize-component';
import { ILoose } from "babylon-lib/tool/utils";
import {
    AbstractAssetTaskInfo, CubeTextureTaskInfo, GLTFMeshTaskInfo, TextureTaskInfo,
    EquiRectangularCubeTextureTaskInfo, HDRCubeTextureTaskInfo, AudioTaskInfo, VideoTextureTaskInfo
} from "babylon-lib/loader/asset-task-info";
import * as Data from '../data/data';
import { CaptureScreen } from 'babylon-lib/highly-coupled-component/capture-screen';
import { Color4 } from "@babylonjs/core/Maths/math.color";
import { DetailView } from "babylon-lib/preset/detail-view";
import { CameraInputPreset } from 'babylon-lib/preset/camera-input-preset';



class DefaultStage extends Stage {

    public scene!: Scene;
    public targetScene!: Scene;
    public camera!: ArcRotateCamera;
    public actor: IActor = {};

    protected assetTaskInfos(): ILoose<AbstractAssetTaskInfo> | Array<IStageAssetsInfo> {
        return {
            changjing: new GLTFMeshTaskInfo(RawAssetsUrl.changjing),

            background: new TextureTaskInfo(RawAssetsUrl.background),
            ground: new TextureTaskInfo(RawAssetsUrl.ground),
            buttonBackground: new TextureTaskInfo(RawAssetsUrl.buttonBackground),
            buttonEditor: new TextureTaskInfo(RawAssetsUrl.buttonEditor),
            buttonSimulation: new TextureTaskInfo(RawAssetsUrl.buttonSimulation),
            buttonClasses: new TextureTaskInfo(RawAssetsUrl.buttonClasses),
            buttonResource: new TextureTaskInfo('../asset/texture/icon/同步资源.png'),
            buttonCloud: new TextureTaskInfo('../asset/texture/icon/微云课.png'),
            buttonMy: new TextureTaskInfo('../asset/texture/icon/我的资源.png'),
            buttonSample: new TextureTaskInfo('../asset/texture/icon/优质案例.png'),
            buttonLearn: new TextureTaskInfo('../asset/texture/icon/知识学习工具.png'),
            buttonGenerate: new TextureTaskInfo('../asset/texture/icon/资源创建工具.png'),

            lightCube: new CubeTextureTaskInfo(
                RawAssetsUrl.lightCubePX,
                RawAssetsUrl.lightCubePY,
                RawAssetsUrl.lightCubePZ,
                RawAssetsUrl.lightCubeNX,
                RawAssetsUrl.lightCubeNY,
                RawAssetsUrl.lightCubeNZ,
            ),

            ERCube: new EquiRectangularCubeTextureTaskInfo(RawAssetsUrl.erCube, 256),

            hdrCube: new HDRCubeTextureTaskInfo(RawAssetsUrl.hdrCube, 512),

            music: new AudioTaskInfo(RawAssetsUrl.music),

            // video:new VideoTextureTaskInfo(RawAssetsUrl.video),
            // movie:new VideoTextureTaskInfo(RawAssetsUrl.movie),

        };
    }

    public render(): void {
        // this.targetScene.render();
        this.scene.render();
    }

    public onWindowResize(rect: DOMRect): void {
        if (this.actor.detailView) this.actor.detailView.windowResizeUpdate();
    }

    protected initializeScenes(): Scene[] {
        const scns = super.initializeScenes();
        this.scene = scns[0];
        this.scene.clearColor = new Color4(0, 0, 0, 0);
        this.targetScene = new Scene(this.engine);
        this.targetScene.clearColor = new Color4(0.1, 0.5, 0.4, 0);
        scns.push(this.targetScene);
        return scns;
    }

    protected async initializeComponents(scenes: Scene[], cameras: Camera[], lights: Light[]): Promise<void> {
        this.camera = cameras[0] as ArcRotateCamera;

        InitializeComponent.initialize(this);
        CaptureScreen.register(this.engine, this.camera, Data.isProduction.value, RawData.appConfig.name);

        this.bindData();
        Data.cameraData.reset(true);
        new CameraInputPreset(scenes[0]);
    }

    private bindData(): void {
        Data.cameraData.onChange(() => {
            this.camera.alpha = Data.cameraAlpha.value;
            this.camera.beta = Data.cameraBeta.value;
            this.camera.radius = Data.cameraRadius.value;
            this.camera.target = Data.cameraTarget.value;
        });
    }

}

interface IActor {
    detailView?: DetailView,
}

export { DefaultStage };